package com.own.core.html.panel;

import java.util.HashMap;
import java.util.Map;

import org.apache.wicket.Component;
import org.apache.wicket.extensions.breadcrumb.IBreadCrumbModel;
import org.apache.wicket.model.IModel;

import com.own.app.OwnSession;
import com.own.core.html.modal.OwnModalWindow;
import com.own.core.html.page.OwnWebPage;
import com.own.core.util.Action;
import com.own.core.object.admin.pages.Page;
 
import com.own.core.operation.DbOperation;
import com.own.core.privilege.AuthorizationPages;
import com.own.core.util.Mode;

public class OwnPanel extends OwnBreadCrumbPanel {

	protected String name;

	// private Class cls;
	private String loadQuery;
	private Page ownPage;
	
	protected Mode mode = Mode.FULL;

	public OwnPanel() {
		this("contentPanel");
		init();
	}

	public OwnPanel(String id, IModel<?> model) {
		super(id, model);
		name = id;

		init();
	}

	public OwnPanel(String id) {
		super(id);
		name = id;

		init();
	}
	

	public OwnPanel(String id,IBreadCrumbModel iBreadCrumb) {
		super(id);
		name = id;
		setBreadCrumbModel(iBreadCrumb);
		init();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void init() {

		this.setOutputMarkupId(true);
		this.setOutputMarkupPlaceholderTag(true);

	}

	public Mode getMode() {
		return mode;
	}

	public void setMode(Mode mode) {
		this.mode = mode;
	}

	public OwnModalWindow getModalWindow() {
		return ((OwnWebPage) getPage()).getModalWindow();
	}

	public Component getMainComponent() {
		return ((OwnWebPage) getPage()).getMainComponent();
	}

	public void setMainComponent(Component component) {
		((OwnWebPage) getPage()).setMainComponent(component);
	}

	public boolean haveAuthorization(Class classObj, Action action) {
		AuthorizationPages authPage = ((OwnSession) getSession()).getAuthPage();

//		authPage.haveAuthorization(classObj.getName(), action);
		return true;
	}

	public Page getOwnPage() {
		if (ownPage == null) {
			return getParentPanel().getOwnPage();

		}
		return ownPage;
	}

	public void setOwnPage(Page ownPage) {
		if (getParentPanel() != null) {
			getParentPanel().setOwnPage(ownPage);

		}
		this.ownPage = ownPage;
	}

	public void setOwnPage(Class cls) {
		Map paraMap = new HashMap();
		paraMap.put("className",cls.getSimpleName());

		this.ownPage =(Page) DbOperation.getME().executeNamedQuery("page.loadBySystemName", paraMap).get(0);
	}

	public Class getCls() {
		if (ownPage == null) {
			return getParentPanel().getCls();
		}
		// } else if (cls != null) {
		// return cls;
		// } else {
		return ownPage.createClass();
		// }

	}

	// public void setCls(Class cls) {
	//
	// if (parentPanel != null) {
	// parentPanel.setCls(cls);
	//
	// }
	// this.cls = cls;
	// }


	public String getLoadQuery() {

		if (!("".equals(loadQuery) || loadQuery == null)) {
			return loadQuery;
		}
		if (ownPage == null) {
			return null;
		}
		return ownPage.getLoadQuery();

	}

	public void setLoadQuery(String loadQuery) {

		this.loadQuery = loadQuery;
	}

 
}
